<template>
  <div>
    <el-dialog title="新增推荐产品" :visible.sync="dialogFormVisible" :before-close="close" :close-on-click-modal="false">
    <el-form :model="form">
      <!-- <el-form-item label="产品名称" :label-width="formLabelWidth">
        <el-input v-model="form.productName" auto-complete="off"></el-input>
      </el-form-item> -->
      <el-form-item label="投资类型" :label-width="formLabelWidth">
        <el-radio-group v-model="form.investType">
          <el-radio  label='0'>首投</el-radio>
          <el-radio  label='1'>复投</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="起投金额" :label-width="formLabelWidth">
        <el-col :span="16">
          <el-input v-model="form.subscriptionOrigin" auto-complete="off"></el-input>
        </el-col>
        <el-col :span="8" style="padding-left: 10px">元</el-col>
      </el-form-item>
      <el-form-item label="投资期限" :label-width="formLabelWidth">
        <el-col :span="16">
          <el-input v-model="form.term" auto-complete="off"></el-input>
        </el-col>
        <el-col :span="8" style="padding-left: 10px">天</el-col>
      </el-form-item>
      <el-form-item label="返利金额" :label-width="formLabelWidth">
        <el-col :span="16">
          <el-input v-model="form.profit" auto-complete="off"></el-input>
        </el-col>
        <el-col :span="8" style="padding-left: 10px">元</el-col>
      </el-form-item>

      <el-form-item label="超级返金额" :label-width="formLabelWidth">
        <el-col :span="16">
          <el-input v-model="form.extraProfit" auto-complete="off"></el-input>
        </el-col>
        <el-col :span="8" style="padding-left: 10px">元</el-col>

      </el-form-item>
      <el-form-item label="是否最高返利" :label-width="formLabelWidth">
        <el-radio v-model="form.isMaxRebate" label="YES">是</el-radio>
        <el-radio v-model="form.isMaxRebate" label="NO">否</el-radio>
      </el-form-item>
      <el-form-item label="综合利率" :label-width="formLabelWidth">
        <el-col :span="16">
          <el-input v-model="form.allInRate" auto-complete="off"></el-input>
        </el-col>
        <el-col :span="8" style="padding-left: 10px">%</el-col>
      </el-form-item>
      <el-form-item label="超级返时间" :label-width="formLabelWidth">
        <el-date-picker
          v-model="extraDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
    </el-dialog>
  </div>
</template>
<script>
import { productSave, productDetail } from 'api/product'
import moment from 'moment'
export default {
  data() {
    return {
      dialogFormVisible: true,
      form: {
      /*
        companyId                      商家Id
        allInRate:                     综合利率
        dateEnd:                       结束时间
        dateStart:                     开始时间
        extraBeginDate:                超级返开始时间
        extraEndDate:                  超级返结束时间
        extraProfit:                   超级返金额
        investType:                    投资类型 首投0  复投1
        productName:                   产品名称
        productType:                   产品类型 推荐0  返利1
        profit:                        返利金额
        profitPercent:                 返利比例
        profitPercentMax:              返利比例上限
        publishStatus:                 产品发布状态
        subscriptionOrigin:            起投金额
        term:                          投资期限
      */
        companyId: sessionStorage.getItem('businessId'),
        dateEnd: '',
        dateStart: '',
        extraBeginDate: '',
        extraEndDate: '',
        extraProfit: '',
        investType: '0',
        productName: '',
        productType: '0',
        profit: '',
        subscriptionOrigin: '',
        term: '',
        isMaxRebate: 'NO',
        allInRate: ''
      },
      extraDate: [],
      date: [],
      formLabelWidth: '120px'
    }
  },
  watch: {
    extraDate(val) {
      if (val === null) {
        this.form.extraBeginDate = ''
        this.form.extraEndDate = ''
      } else {
        this.form.extraBeginDate = moment(val[0]).format('YYYY-MM-DD HH:mm:ss')
        this.form.extraEndDate = moment(val[1]).format('YYYY-MM-DD HH:mm:ss')
      }
    }
  },
  created() {
    if (sessionStorage.getItem('productId')) {
      this.getProductDetail()
    }
  },
  methods: {
    async getProductDetail() {
      try {
        let back = await productDetail(sessionStorage.getItem('productId'))
        back = back.data
        if (back.resultCode === '200') {
          console.log(back.result)
          this.form.companyId = back.result.companyId
          this.form.productName = back.result.productName
          this.form.dateStart = back.result.dateStart
          this.form.dateEnd = back.result.dateEnd
          this.form.extraProfit = back.result.extraProfit
          this.form.profit = back.result.profit
          this.form.subscriptionOrigin = back.result.subscriptionOrigin
          this.form.term = back.result.term
          this.form.productId = back.result.productId
          this.form.isMaxRebate = back.result.isMaxRebate
          this.form.allInRate = back.result.allInRate
          this.form.investType = back.result.investType.toString()
          if (back.result.extraBeginDate == null || back.result.extraBeginDate === '' || back.result.extraEndDate === null || back.result.extraEndDate === '') {
            this.extraDate = null
          } else {
            this.extraDate = [back.result.extraBeginDate, back.result.extraEndDate]
          }
        }
      } catch (err) {
        this.$alert('请求出错' + err, '提示')
      }
    },
    close() {
      this.$emit('close')
    },
    async submit() {
      var regInteger = /^[1-9]\d*$/

      var regInteger02 = /^[0-9]\d*$/

      var regDecimal2 = /^[0-9]+(.[0-9]{1,2})?$/

      if (this.form.subscriptionOrigin === '' || this.form.subscriptionOrigin === null || regInteger.test(this.form.subscriptionOrigin) === false) {
        this.$alert('起投金额格式错误, 请输入正整数', '提示')
        return
      }

      if (this.form.term === '' || this.form.term === null || regInteger.test(this.form.term) === false) {
        this.$alert('投资期限格式错误, 请输入正整数', '提示')
        return
      }

      if (this.form.profit === '' || this.form.profit === null || regInteger.test(this.form.profit) === false) {
        this.$alert('返利金额格式错误, 请输入正整数', '提示')
        return
      }

      if (this.form.extraProfit !== null && this.form.extraProfit !== '' && regInteger02.test(this.form.extraProfit) === false) {
        this.$alert('超级返利金额格式错误, 请置空或输入正整数', '提示')
        return
      }

      if (this.form.allInRate === '' || this.form.allInRate === null || regDecimal2.test(this.form.allInRate) === false) {
        this.$alert('综合利率格式错误, 请输入整数或两位小数', '提示')
        return
      }

      this.form.companyId = sessionStorage.getItem('businessId')
      try {
        let back = await productSave(this.form)
        back = back.data
        if (back.resultCode === '200') {
          this.$emit('update')
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
      this.dialogFormVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
